package org.gilgamesh.stool.core.collection;

import cn.hutool.core.collection.CollUtil;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 集合工具类
 *
 * @author Gilgamesh
 * @since 2021-08-20
 **/
public class SuperCollectionUtil extends CollUtil {

    /**
     * 数组转list
     **/
    public static <E> List<E> asList(E[] array) {
        List<E> list = new ArrayList<>();
        for (E e : array) {
            list.add(e);
        }
        return list;
    }

    /**
     * list转数组
     **/
    public static <E extends Object> E[] toArray(List<E> list) {
        if (isNotEmpty(list)) {
            E e = list.get(0);
            E[] es = (E[]) Array.newInstance(e.getClass(), list.size());
            return list.toArray(es);
        }
        return null;
    }
}
